﻿using System;
using System.Data;
using System.Text;

namespace ltModel.DAL
{
    /// <summary>
    /// 数据访问类vote。
    /// </summary>
    public class vote
    {
        public vote()
        { }
        #region  成员方法

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from lt_vote where [id]=" + id + "");
            return DataObject.Exists(strSql.ToString());
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(ltModel.Model.vote model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into [lt_vote](");
            strSql.Append("[vote],[class],[count],[color]");
            strSql.Append(")");
            strSql.Append(" values (");
            strSql.Append("'" + model.Vote + "',");
            strSql.Append("" + model.Class + ",");
            strSql.Append("" + model.count + ",");
            strSql.Append("'" + model.color + "'");
            strSql.Append(")");
            strSql.Append(";select @@IDENTITY");
            object obj = DataObject.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 1;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(ltModel.Model.vote model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update [lt_vote] set ");
            strSql.Append("[vote]='" + model.Vote + "',");
            strSql.Append("[class]=" + model.Class + ",");
            strSql.Append("[color]='" + model.color + "'");
            strSql.Append(" where [id]=" + model.id + "");
            if (DataObject.ExecuteSql(strSql.ToString()) > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 投票
        /// </summary>
        /// <param name="id">选项ID</param>
        /// <returns></returns>
        public bool VoteAdd(string id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update [lt_vote] set [count]=[count]+1 ");
            if (id != "")
                strSql.Append(" where [id] in(" + id + ") ");
            if (DataObject.ExecuteSql(strSql.ToString()) > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete lt_vote ");
            strSql.Append(" where [id]=" + id);
            if (DataObject.ExecuteSql(strSql.ToString()) > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 删除主题类别数据
        /// <param name="id">主题ID</param>
        /// </summary>
        public bool DeleteCid(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete lt_vote ");
            strSql.Append(" where [class]=" + id);
            if (DataObject.ExecuteSql(strSql.ToString()) > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ltModel.Model.vote GetModel(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  ");
            strSql.Append(" [id],[vote],[class],[count],[color] ");
            strSql.Append(" from [lt_vote] ");
            strSql.Append(" where [id]=" + id);
            ltModel.Model.vote model = new ltModel.Model.vote();
            DataSet ds = DataObject.Query(strSql.ToString());
            model.id = id;
            if (ds.Tables[0].Rows.Count > 0)
            {
                model.Vote = ds.Tables[0].Rows[0]["vote"].ToString();
                if (ds.Tables[0].Rows[0]["class"].ToString() != "")
                {
                    model.Class = int.Parse(ds.Tables[0].Rows[0]["class"].ToString());
                }
                if (ds.Tables[0].Rows[0]["count"].ToString() != "")
                {
                    model.count = int.Parse(ds.Tables[0].Rows[0]["count"].ToString());
                }
                model.color = ds.Tables[0].Rows[0]["color"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select [id],[vote],[class],[count],[color] ");
            strSql.Append(" FROM [lt_vote] ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DataObject.Query(strSql.ToString());
        }

        #endregion  成员方法
    }
}